<?php

declare(strict_types=1);

namespace App\Libs\Wx;

use EasyWeChat\OfficialAccount\Application;

class WxSdkEasy
{
    protected array $wxConfig;
    protected object $wx;

    public function __construct (string $confDef='wx_config')
    {
        $this->wxConfig = [
            'app_id' => config($confDef)['app_id'],
            'secret' => config($confDef)['app_secret'],
        ];
        $this->wx = new Application([
            'app_id' => config($confDef)['app_id'],
            'secret' => config($confDef)['app_secret'],
        ]);
    }

    /**
     * 微信网页授权
     * @param string $url
     * @param string $snsapi
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function authWeb(string $url,string $snsapi = 'snsapi_userinfo'): string
    {
        return $this->wx->getOAuth()->scopes([$snsapi])->redirect($url);
    }

    /**
     * 微信信息获取
     * @param string $code
     * @return object
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function userInfo(string $code): object
    {
        return $this->wx->getOAuth()->userFromCode($code);
    }

}